/*
 * Copyright(C) 2012 Luvina Software Company
 *
 * PasswordValidator.java, Jun 7, 2012 HaiPD
 */
package net.luvina.manageuser.validator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import net.luvina.manageuser.util.Message;
import net.luvina.manageuser.util.StringUtill;

/**
 * PasswordValidator
 *
 * @author HaiPD
 *
 */
public class PasswordValidator implements Validator {

	private static final int MIN_LENGTH = 5;
	private static final int MAX_LENGTH = 15;

	// private static final String PASSWORD_PATTERN =
	// "(?!^[0-9]*$)(?!^[a-zA-Z!@#$%^&*()_+=<>?]*$)^([a-zA-Z!@#$%^&*()_+=<>?0-9])$";

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * javax.faces.validator.Validator#validate(javax.faces.context.FacesContext
	 * , javax.faces.component.UIComponent, java.lang.Object)
	 */
	@Override
	public void validate(FacesContext context, UIComponent component,
			Object object) throws ValidatorException {
		String value = object.toString();
		String labelField = (String) component.getAttributes().get("label");
		if (StringUtill.checkMinMaxLeng(value, MIN_LENGTH, MAX_LENGTH)) {
			FacesMessage msg_er007 = new FacesMessage("「" + labelField + "」 を"
					+ MAX_LENGTH + "＜＝桁数、＜＝ " + MIN_LENGTH
					+ Message.getString("ER007"));
			msg_er007.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(msg_er007);
		}

		if (StringUtill.isTwoByteCharater(value)) {
			FacesMessage msg_er007 = new FacesMessage("「" + labelField + "」 "
					+ Message.getString("ER008"));
			msg_er007.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(msg_er007);
		}

		// boolean matcher = Pattern.matches(PASSWORD_PATTERN, value);
		// if (!matcher) {
		// FacesMessage msg_er007 = new FacesMessage("「" + labelField + "」 "
		// + Message.getString("ER008"));
		// msg_er007.setSeverity(FacesMessage.SEVERITY_ERROR);
		// throw new ValidatorException(msg_er007);
		// }
	}
}
